import {
  defineStore
} from 'pinia'

export const userStore = defineStore('user', {
  state: () => {
    return {
      token: '',
      userInfo: {},
      configMap: {}
    }
  },
  actions: {
    setToken(token) {
      this.token = token
      sessionStorage.setItem('BI_token', token)
    },
    setConfig(configMap) {
      this.configMap = configMap
      sessionStorage.setItem('BI_configMap', JSON.stringify(configMap))
    },
    setUserInfo(userInfo) {
      this.userInfo = userInfo
      sessionStorage.setItem('BI_userInfo', JSON.stringify(userInfo))
    }
  },
  // 开启持久化
  persist: {
    enabled: true, // 启用
    strategies: [
      // storage 可选localStorage或sessionStorage
      // paths 给指定数据持久化
      {
        key: 'user',
        storage: localStorage,
        paths: ['token', 'userInfo']
      }
    ]
  },
  getters: {
    getToken(state) {
      return state.token
    },
    getUserInfo(state) {
      return state.userInfo
    }
  }
})
